/****************************************************************************

 generic-gtk.cpp - A Generic GktUI for LV2

Copyright (C) Michael R Fisher <mfisher31@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 01222-1307 USA

****************************************************************************/
#include <iostream>
#include <gtkmm.h>

#include "headers/lv2_generic_ui.h"
#include <lv2gui.hpp>

#include "main-widget.hpp"

using namespace Gtk;
using namespace LV2;

template <bool Required = true>
  struct GenericUI {

    /** This is the type that your plugin or GUI class will inherit when you
        use the GenericUI mixin. The public and protected members defined
        here will be available in your plugin class.
    */
    template <class Derived> struct I : public Extension<Required> {

      /** @internal */
      I() : m_host_support(true),m_bundle("") { }

        /** @internal */
        static void map_feature_handlers(FeatureHandlerMap& hmap) {
          hmap[LV2_GENERIC_UI_URI] = &I<Derived>::handle_feature;
        }

        /** @internal */
        static void handle_feature(void* instance, void* data) {
          Derived* d = reinterpret_cast<Derived*>(instance);
          I<Derived>* e = static_cast<I<Derived>*>(d);
          e->m_hdesc = static_cast<LV2_Generic_UI_Feature*>(data);
          if(e->m_hdesc)
           // m_bundle = e->m_hdesc->plugin_bundle;
          e->m_ok = (e->m_hdesc != 0);
          e->m_host_support = (e->m_hdesc != 0);
        }

         /** @internal
            Returns the plugin descriptor for this extension.
        */
        static void const* extension_data(char const* uri) {
          return NULL;
        }

    protected:
        /** @internal */
        const char* plugin_bundle() {
          return m_hdesc->plugin_bundle;
        }

        bool host_supports_genui() {
        	return m_host_support;
        }
     private:

        LV2_Generic_UI_Feature* m_hdesc;
        std::string m_bundle;
        bool m_host_support;

    };

  };


class GtkUI : public LV2::GUI<GtkUI, GenericUI<true> > {
private:
  MainWidget    widget;
  Plugin        plugin;
  std::string   m_bundle;



public:
  GtkUI(const char* plugin_uri)
  : m_bundle(bundle_path()),plugin(plugin_uri),
    widget(bundle_path())
  {
    if(!plugin.open_bundle(plugin_bundle())) {
      #ifndef NDEBUG
        std::cout <<"Bundle not good\n";
      #endif
    } else {

      sigc::slot<void,Port&> func = sigc::mem_fun(widget,&MainWidget::add_control);
      plugin.signal_found_control.connect(func);
      plugin.find_controls();

      widget.set_plugin_info(plugin.get_info());
      widget.set_symbol_map(plugin.get_symbol_map());
      widget.signal_write_control.connect(mem_fun(*this, &GtkUI::write_control));
    }

    pack_start(widget);
  }

protected:


};

uint32_t _ = GtkUI::register_class("http://djaxetota.com/lv2/interfaces/generic-gtk");
